|
|
|
|
|
|
|
Solution 9
Translating DEVMODE |
|
|
|
|
|
|
|
|
When you look at data within a structure and the values don't seem to make sense, chances are that there is a problem in the structure declaration in which one of the variables in your declaration has the wrong size. There is little that you can do wrong with simple numeric declarations. But strings and arrays can get tricky. For example, the dmDeviceName field is defined in C as follows: |
|
|
|
|
|
|
|
|
#define CCHDEVICENAME 32
BYTE dmDeviceName[CCHDEVICENAME]; |
|
|
|
|
|
|
|
|
At first glance, the following VB declaration looks correct: |
|
|
|
|
|
|
|
|
Private Const CCHDEVICENAME = 32
dmDeviceName(CCHDEVICENAME) As Byte |
|
|
|
|
|
|
|
|
But it isn't correct. Why? Consider: How long is the array? |
|
|
|
|
|
|
|
|
The array bounds in a C++ language array define the number of elements in the array. In this case, the array holds 32 bytes. But the array bounds in a Visual Basic language array define the upper bound of the array. If the first element in the array is 0 and the upper bound is 32 (as shown here), the total number of elements is 33! |
|
|
|
|
|
|
|
|
One solution is to change the VB declaration to |
|
|
|
|
|
|
|
|
dmDeviceName(CCHDEVICENAME-1) As Byte |
|
|
|
|
|
|
|
|
But there's an easier solution. Since this entry is designed to contain a string anyway, you can simply use a fixed-length string. Unlike dynamic strings, which are stored as BSTR pointers (32-bit pointers), a fixed-length string is stored directly within the structure. So if you declare the string as follows, |
|
|
|
|
|
|
|
|
Dim dmDeviceName As String * CCHDEVICENAME |
|
|
|
|
|
|
|
|
it will take up 32 bytes in the structure. |
|
|
|
|
|
|
|
|
Change the dmFormName field in the same way, and the function will work flawlessly. |
|
|
|
|
|